<?php if(!defined('ARCH_ROOT_PATH')) exit;
/**
 *	Architect Framework
 *
 *	Architect Framework is a light weight object oriented web applications framework written for PHP 5.1.4 and later that provide the tools to develop and deploy a website or application fast and with as little code as possible.
 *
 *	@author Robin Grass, <hello@coffinboy.se>
 *	@link http://architect.kodlabbet.net
 *	@license http://www.opensource.org/licenses/gpl-3.0.html GNU General Public License (v. 3)
 */

/**
 *	Architect_TemplateAbstract
 *
 *	This template abstract registers a new template protocol.
 *
 *	@package Abstracts
 *	@version 1.0
 *	@author Robin Grass, <hello@coffinboy.se>
 */
class Architect_TemplateAbstract {

	/**
	 *	@var string $prev_template
	 */
	protected $prev_file;
	
	/**
	 *	@var string $template
	 */
	protected $template;
	
	/**
	 *	@var string $wrapper
	 */
	protected $wrapper;
	
	/**
	 *	@var string $include_path
	 */
	protected $include_path;
	
	/**
	 *	__construct
	 */
	abstract public function __construct();
	
	/**
	 *	setIncludePath
	 *
	 *	@param string $include_path
	 */
	public function setIncludePath($include_path) {
		$this->include_path = $include_path;
	}
	
	/**
	 *	setStreamWrapper
	 *
	 *	@param string $wrapper
	 *	@param string $stream_class
	 *
	 *	@return bool true
	 */
	public function setStreamWrapper($wrapper, $stream_class) {
		if(!in_array($wrapper, stream_get_wrappers())) {
			if(!stream_wrapper_register($wrapper, $stream_class))
				throw new Architect_Exception("Cannot register {$wrapper}:// stream wrapper.");
		}
		$this->wrapper = $wrapper;
	return true;
	}
	
	/**
	 *	setTemplate
	 *
	 *	@param string $template
	 */
	public function setTemplate($template) {
		if(!is_readable($this->include_path . $template))
			throw new Architect_Exception("Can't read file: {$template}.");
		$this->template = $template;
	}
	
	/**
	 *	render
	 */
	public function render() {
		if($this->template == $this->prev_file)
			throw new Architect_Exception("View render loop in {$this->template}.");
		
		$this->prev_file = $this->template;
		require $this->wrapper . '://' . $this->include_path . $this->template;
	}
	
	/**
	 *	bufferRender
	 *
	 *	@param string $file
	 */
	protected function bufferRender($file) {
		ob_start();
		$this->renderFile($this->include_path . $file);
	return ob_get_clean();
	}

}
?>